import { Space, Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import React from 'react';

//定义表格中每一条数据的对象结构
interface DataType {
  key: string;
  name: string;
  age: number;
  address: string;
  tags: string[];
}

//定义一个数组, 该数组用来定义表格的『结构』
const columns: ColumnsType<DataType> = [
  {
    title: '姓名',
    dataIndex: 'name', //设置显示该列单元格 『对象中对应属性名』
    key: 'name',
    render: text => <a>{text}</a>,  //回调函数的值, 用来对结果值进行调整, 返回虚拟 DOM 对象
  },
  {
    title: '年龄',
    dataIndex: 'age',
  },
  {
    title: '地址',
    dataIndex: 'address',
  },
  {
    title: '标签',
    dataIndex: 'tags',
    render: (_, { tags }) => {
      return <>
        {tags.map(tag => {
          let color = tag.length > 5 ? 'geekblue' : 'green';
          if (tag === 'loser') {
            color = 'volcano';
          }
          return (
            <Tag color={color} key={tag}>
              {tag.toUpperCase()}
            </Tag>
          );
        })}
      </>
    }
  },
  {
    title: '操作',
    render: (_, record) => (
      <Space size="middle">
        <a>Invite {record.name}</a>
        <a>Delete</a>
      </Space>
    ),
  },
];

//表格的数据
const data: DataType[] = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
    tags: ['nice', 'developer'],
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
    tags: ['loser'],
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park',
    tags: ['cool', 'teacher'],
  },
];

const TableCom: React.FC = () => {
  // columns 控制表格的结构  dataSource 设置表格的数据
  return <Table columns={columns} dataSource={data} />
}

export default TableCom;